<?php
require_once ('pagination.php');
require_once ('create-message.php');
define( "DATABASE_SERVER", "localhost" );
	define( "DATABASE_USERNAME", "root" );
	define( "DATABASE_PASSWORD", "" );
	define( "DATABASE_NAME", "autogator_test" );
//connect to the database
	$mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD) or die(mysql_error());
	//select the database
	mysql_select_db( DATABASE_NAME );

session_start();
?>
<?php
	if(!isset($_GET["page"]) &&  !isset($_GET["key"]))
	{
		$_SESSION['key'] = '';
	}
 	$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
	$page = ($page == 0 ? 1 : $page);
	$perpage = 20;//limit in each page
	$startpoint = ($page * $perpage) - $perpage;
	$delno = 0;
	if(isset($_POST['restore']))
	{
		$fruit = $_POST["check"];
		$delno = count($fruit);
		$myDate = date('Y-m-d H:i:s');
        if (count($fruit)>0) {
                
				for ($i=0; $i<count($fruit); $i++) 
				{	
$query_1 = "Update vehicle_backup Set `current_action` = 'Restored',`status` = 'Restored', update_date = '".$myDate."' Where vehicle_id=".$fruit[$i];
					  $query1 = mysql_query($query_1) or die('Query Error2h'.$query_1);
				}
				
				$file_content = file('restore_query.sql');
				$query_1 = "";
				foreach($file_content as $sql_line){
				  if(trim($sql_line) != "" && strpos($sql_line, "--") === false){
					$query_1 .= $sql_line;
					if (substr(rtrim($query_1), -1) == ';'){
					  $result = mysql_query($query_1)or die(mysql_error());
					  $query_1 = "";
					}
				  }
				}
        }
	}
	else if(isset($_POST['search'])) {
	   $_SESSION['key'] = $_POST["key"];
	   header('location: index.php?key='.$_SESSION['key'].'&page=1');
   }else if(isset($_POST['vehicle_id'])) {
	   sortFields('vehicle_id');
   }else if(isset($_POST['year'])) {
	   sortFields('year');
   }else if(isset($_POST['make'])) {
	   sortFields('make');
   }else if(isset($_POST['model'])) {
	   sortFields('model');
   }else if(isset($_POST['first_name'])) {
	   sortFields('first_name');
   }else if(isset($_POST['state'])) {
	   sortFields('state');
   }else if(isset($_POST['update_date'])) {
	   sortFields('update_date');
   }
   
   function sortFields($field){
	   if( $_REQUEST['field'] == $field && $_REQUEST['sort'] == 'ASC'){
		  $_REQUEST['sort'] = 'DESC';
	   }else{
		   $_REQUEST['field'] = $field;
	   	   $_REQUEST['sort'] = 'ASC';
	   }
	   header('location: index.php?key='.$_REQUEST['key'].'&page=1&field='.$_REQUEST['field'].'&sort='.$_REQUEST['sort']);
   }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Vehicle's Bid Informations</title>
<link href="css/main.css" rel="stylesheet" type="text/css"></link>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-21110030-5']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
 function confirmWindow()
 {
	 var answer = confirm('Do you want to continue?')
	 
	 if(answer)
	 {
		Alert('test');
		/*var str = [];
    	for(x=0; x<document.MyForm.elements["check[]"].length; x++){
			if(document.MyForm.elements["check[]"][x].checked==true){
				str[x] = document.MyForm.elements["check[]"][x].value + ',';
			}
    	}*/
 		//document.MyForm.action = 'process.php?values[]='+str;
		
		//document.MyForm.submit();
		return true;
	 }
	 else
	 {
		 return false;
	 }
 }
</script>
<style type="text/css">
a {
color:#333;
text-transform: capitalize;
}
a:hover{
color: #999;
text-decoration:underline
}
</style>
<style type="text/css">
body p {
	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
	font-size: 14px;
}
.heading {
	font-size: 20px;
}
.price-li {
	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
	font-size:14px;
}


</style>
</head>
<body>
<div class="boxBlack">
<div class="boxBlackTl"><div class="boxBlackTr">
  <h2><strong class="heading">Autogator Data Archive Manager</strong></h2>
</div></div>

<div class="boxBodyBlack">
<form action="" method="POST" name="MyForm1">
<div class="search">
	<label>Search by: </label>
	<input type="text" name="key" id="key" />
    <input type="hidden" name="currenyKey" id="currentKey" />
	<input type="submit" name="search" value="Search"/>
    
</div>
</form>
  <div class="boxLblue floatLeft" style="width:100%;">
 
  <div class="boxLblueTl">
  <form action="" method="POST" name="MyForm3">
  <div class="boxLblueTr">
  		<div class="sort">
        	<!--  <label>Order by: </label>
            <select name="field_name">
              <option value="null"></option>
              <option value="vehicle_id">Bid #</option>
              <option value="make">Make</option>
              <option value="model">Model</option>
              <option value="date">Bid Date</option>
            </select>
             <select  name="sort_type">
              <option value="ASC">Ascending</option>
              <option value="DESC">Descending</option>
            </select>
            <input type="submit" name="getSort" value="Sort"/>-->
            <table cellpadding='2'><tr>
            	<td width="30"></td>
				<td width="60" align="left"><input type='image' src='images/bid.png' value='Bid #' name='vehicle_id'/></td>
                <td width="60" align="left"><input type='image' src='images/year.png' value='Year' name='year'/></td>
                <td width="150" align="left"><input type='image' src='images/make.png' value='Make' name='make'/></td>
                <td width="150" align="left"><input type='image' src='images/model.png' value='Model' name='model'/></td>
                <td width="150" align="left"><input type='image' src='images/seller.png' value='Seller' name='first_name'/></td>
                <td width="150" align="left"><input type='image' src='images/city.png' value='City/State' name='state'/></td>
                <td width="150" align="left"><input type='image' src='images/date.png' value='Update Date' name='update_date'/></td>
			</tr></table>
        </div>
  </div>
  </form>
  </div>
 <form action="" method="POST" name="MyForm" onsubmit="return confirmWindow();">

<div class="boxBodyLblue">

 <?php
 	if($delno)
	{
		echo "<font color='#990000'/><b>".$delno." Data Restored</font>";
		$delno= 0;
	}
	$resultKey = $_SESSION['key'];
	
  	echo createThings($startpoint, $perpage, $resultKey);
  ?> 

  <div class="page">
  <?php

	$path = "index.php?key=".$_SESSION['key']."&field=".$_REQUEST['field']."&sort=".$_REQUEST['sort']."&";
	$c_key = $_SESSION['key'];
  	echo Pages("vehicle_backup", $perpage, $path, $c_key);	
  ?>
  </div>
  <div class="clearBoth"></div>
    <div class="restoreButton"> <input type="submit" value="Restore" name="restore"/></div>

</div>
</form>
<div class="boxLblueBl"><div class="boxLblueBr"></div></div>
</div>
  <div class="clearBoth"></div>
  <div class="clearBoth"></div>
</div>
<div class="boxBlackBl"><div class="boxBlackBr"></div></div>
</div>

</body></html>